﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class Vendor : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", 0, "selectwhereid");
                    ListItem lt = new ListItem("Chon danh muc", "-1");
                    Cate1.Items.Add(lt);
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                        Cate1.Items.Add(lt);
                    }
                    Cate1.SelectedIndex = 0;
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }         
        }
        protected void Cate1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int index = Cate1.SelectedIndex;
                if (index > 0)
                {
                    int id = Convert.ToInt32(Cate1.SelectedValue);
                    DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Vendor", "*", "Category_ID1", id, "selectwhereid");

                    rpt_Vendor.DataSource = dt;
                    rpt_Vendor.DataBind();
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }          
        }

        protected void rpt_Vendor_ItemCommand1(object source, RepeaterCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "delete")
                {
                    int id = Convert.ToInt32(e.CommandArgument.ToString());
                    DAL.DCollector.DBCollector.Instance.StoredProcedureDeleteByRows("tbl_Vendor", "Vendor_ID", id.ToString());
                    Response.Redirect("/Admin/Index.aspx?panel=1&sub=2");
                }
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }           
        }
    }
}